package com.ag.work.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ag.bean.TbExpert;
import com.ag.bean.TbExpertArea;
import com.ag.bean.TbUser;
import com.ag.common.BaseAction;
import com.ag.common.IPermissionService;
import com.ag.work.service.IAdviceApplyService;
import com.ag.zlw.action.ExpertAreaAction;
import com.ag.zlw.service.ExpertAreaService;

/**
 * 
 * @author dgm 处置建议的申请，也即是把处置建议申请内容插入
 * 
 */
@Controller("adviceApplyAction")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class AdviceApplyAction extends BaseAction implements SessionAware {

	@Resource(name = "adviceApplyServiceImpl")
	private IAdviceApplyService service;

	// 用来得到用户
	@Resource(name = "PermissionServiceImpl")
	private IPermissionService pms;

	private List<TbExpertArea> expertAreaList; // 专家领域LIST
	private String result;
	private String expertAreaIdString; // 专家领域id
	private List<TbExpert> expertList; // 专家LIST

	private String eventIdString; // 事件id
	private String adviceExpertIds; // 建议专家id,这里有多个参数,形式如(XX,XX,XXX,),在service中处理
	private String lastestTime; // 最迟提交时间
	private String applyString; // 申请建议请求
	private String applyPersonId; // 申请人id

	private Map<String, Object> sessionMap;

	public String getApplyString() {
		return applyString;
	}

	public Map<String, Object> getSessionMap() {
		return sessionMap;
	}

	public void setSessionMap(Map<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
	}

	public void setApplyString(String applyString) {
		this.applyString = applyString;
	}

	public String getLastestTime() {
		return lastestTime;
	}

	public void setLastestTime(String lastestTime) {
		this.lastestTime = lastestTime;
	}

	public String getEventIdString() {
		return eventIdString;
	}

	public void setEventIdString(String eventIdString) {
		this.eventIdString = eventIdString;
	}

	public String getAdviceExpertIds() {
		return adviceExpertIds;
	}

	public void setAdviceExpertIds(String adviceExpertIds) {
		this.adviceExpertIds = adviceExpertIds;
	}

	public List<TbExpert> getExpertList() {
		return expertList;
	}

	public void setExpertList(List<TbExpert> expertList) {
		this.expertList = expertList;
	}

	public String getExpertAreaIdString() {
		return expertAreaIdString;
	}

	public void setExpertAreaIdString(String expertAreaIdString) {
		this.expertAreaIdString = expertAreaIdString;
	}

	public List<TbExpertArea> getExpertAreaList() {
		return expertAreaList;
	}

	public void setExpertAreaList(List<TbExpertArea> expertAreaList) {
		this.expertAreaList = expertAreaList;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	
	
	/**
	 * 得到用户信息
	 */

	public String findUser() {
		System.out.println("这是findUser_eventReportAction方法");
		TbUser tbUser = (TbUser) pms.getCurrentUser(sessionMap);
		System.out.println(tbUser.getUId()+"*********");
		return tbUser.getUId();
	}
	/**
	 * 查找专家领域
	 * 
	 * @return
	 */
	public String findExpertArea() {
		System.out.println("这是findExpertArea_action方法");
		result = "查找专家领域表成功";

		try {
			expertAreaList = service.findExpertArea();
			System.out.println(expertAreaList.size() + "*********");
		} catch (Exception e) {
			result = "查找专家领域表不成功";
			e.printStackTrace();
		}
		return "findExpertArea";
	}

	/**
	 * 通过专家领域找专家们
	 * 
	 * @return
	 */

	public String findExpertByArea() {
		System.out.println("这是findExpertByArea_action方法");
		result = "查找专家成功";
		try {
			expertList = service.findExpertByArea(expertAreaIdString);
		} catch (Exception e) {
			result = "查找专家失败";
			e.printStackTrace();
		}
		return "findExpertByArea";
	}
	
	/**
	 * 申请处置建议
	 * @return
	 */

	public String applyAdvice() {
		System.out.println("这是applyAdvice_action方法");
		result = "申请处置建议成功";
		applyPersonId=findUser();
		try {
			service.applyAdvice(eventIdString, adviceExpertIds, lastestTime,
					applyString, applyPersonId);
		} catch (Exception e) {
			result = "申请处置建议失败";
			e.printStackTrace();
		}
		try {
			service.sendMessageAndLog(eventIdString,adviceExpertIds,applyString,applyPersonId);
		} catch (Exception e) {
			result="短信发送失败";
			e.printStackTrace();
		}
		return "applyAdvice";
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.sessionMap = session;

	}
}
